<?php

require_once dirname(__FILE__).'/../lib/photoGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/photoGeneratorHelper.class.php';

/**
 * photo actions.
 *
 * @package    sanziniafilms
 * @subpackage photo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class photoActions extends autoPhotoActions{
	public function executeDelete(sfWebRequest $request){
		$this->forward404Unless($photo = Doctrine_Core::getTable('photo')->find(array($request->getParameter('id'))), sprintf('Object photo does not exist (%s).', $request->getParameter('id')));
        //Averiguamos el album al que pertenece la foto para redirigir
		$albumId = $photo->getTreeId();
		//unlink(sfConfig::get('sf_upload_dir').'/assets/'.$photo->getImagen());
		unlink(sfConfig::get('sf_upload_dir').'/assets/'.$photo->getImagen());
		//unlink(sfConfig::get('sf_upload_dir').'/assets/thumbs/'.$photo->getImagen());
		unlink(sfConfig::get('sf_upload_dir').'/assets/thumbs/'.$photo->getImagen());
		$photo->delete();
		$this->redirect('@album?id='.$albumId);
	}
	
	public function executeAlbum(sfWebRequest $request){
		// sorting
		if ($request->getParameter('sort') && $this->isValidSortColumn($request->getParameter('sort')))
		{
		  $this->setSort(array($request->getParameter('sort'), $request->getParameter('sort_type')));
		}

		if($request->getParameter('id')!=null){
			$album = Doctrine::getTable('Tree')->findOneById($request->getParameter('id'));
			$this->albumNombre = $album->getTitulo();
		}
		else{
			$this->albumNombre = "";
		}
		
		$this->treeObject = Doctrine_Core::getTable('Tree')->getTree();
		$this->rootColumnName = $this->treeObject->getAttribute('id');

		$this->pager = new sfDoctrinePager('Photo', 800);

		$this->pager->setQuery(Doctrine::getTable('Photo')->createQuery('p')->where('p.tree_id = ?', $request->getParameter('id'))->orderBy('orderid'));

		$this->pager->setPage($request->getParameter('page', 1));

		$this->pager->init();

		$this->sort = $this->getSort();
	}
	
	
	
	public function executeNew(sfWebRequest $request)
	{
		if($request->getParameter('idAlbum')!=null){
			$album = Doctrine::getTable('Tree')->findOneById($request->getParameter('idAlbum'));
			$this->albumNombre = $album->getTitulo();
		}
		$this->form = $this->configuration->getForm();
		$this->photo = $this->form->getObject();
	}	
	
	public function executeEdit(sfWebRequest $request)
  {
    $this->photo = $this->getRoute()->getObject();
	
			$this->album = Doctrine::getTable('Tree')->findOneById($this->photo['tree_id']);
			//$this->albumNombre = $album->getTitulo();
	
    $this->form = $this->configuration->getForm($this->photo);
  }
  
	public function executeProcessSortable($request){
		if ($request->isXmlHttpRequest()){
			foreach ($_GET['listItem'] as $position => $item){
			  $q=Doctrine_Query::create()
			  ->update('Photo v')
			  ->set('v.orderId', $position)
			  ->where('v.id = ?', $item)
			  ->execute();
			} 
			return $this->renderText("e");
		}
	}

}
